import { loginAPI } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '' // 设置token的初始值
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token) // 将token存储到本地
    },
    // 删除token
    removeToken(state) {
      state.token = ''
      removeToken()
    }
  },
  actions: {
    async login(context, { username, password }) {
      const res = await loginAPI({ username, password })
      // console.log(res)
      // 提交到，mutations中
      context.commit('setToken', res.data.token)
    },
    // 退出登录,提交到，mutations中
    logout(context) {
      context.commit('removeToken')
    }
  }
}
